/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetluc;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 *
 * @author Lone_Wolf
 */
public class Search extends JScrollPane {

    private Fenetre fenetre;
    private ArrayList<SearchResult> arrResultats;
    private ArrayList<SearchResult> arrResultats2;
    private JPanel pnlVisible;
    private String input;
    private boolean[] params;
    private GridBagLayout gbl;
    private GridBagConstraints gbc;

    public Search(Fenetre fenetre, String input, boolean[] params) {
        this.fenetre = fenetre;
        this.input = input.toLowerCase();
        this.params = params;

        arrResultats = new ArrayList<SearchResult>();
        arrResultats2 = new ArrayList<SearchResult>();
        pnlVisible = new JPanel();
        pnlVisible.setPreferredSize(new Dimension(450, 280));
        this.setPreferredSize(new Dimension(460, 303));
        this.setViewportView(pnlVisible);

        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();
        pnlVisible.setLayout(gbl);
        gbc.fill = GridBagConstraints.BOTH;

        categories();
    }

    private void categories() {
        if (!input.isEmpty()) {
            if (params[0]) {
                searchClients();
            }
            if (params[1]) {
                searchFactureNumero();
            }
            if (params[2]) {
                searchEmploye();
            }
            if (params[3]) {
                searchEntreeClient();
            }
            if (params[4]) {
                searchEntreeEmployé();
            }
            if (params[5]) {
                //searchEntreeDate();
            }
            if (params[6]){
                searchFactureClient();
            }
        }
    }

    private void searchClients() {
        for (int i = 0; i < fenetre.arClient.size(); i++) {
            if (fenetre.arClient.get(i).getNom().toLowerCase().contains(input)) {
                //ajoutResultat(new SearchResult(TypeDonnees.CLIENT, fenetre.arClient.get(i)));
                ajoutResultat2(new SearchResult(TypeDonnees.CLIENT, fenetre.arClient.get(i)));
            } else if (fenetre.arClient.get(i).getCode().contains(input)) {
                //ajoutResultat(new SearchResult(TypeDonnees.CLIENT, fenetre.arClient.get(i)));
                ajoutResultat2(new SearchResult(TypeDonnees.CLIENT, fenetre.arClient.get(i)));
            }
        }
    }

    private void searchEmploye() {
        for (int i = 0; i < fenetre.arEmploye.size(); i++) {
            if (fenetre.arEmploye.get(i).getNom().toLowerCase().contains(input)) {
                //ajoutResultat(new SearchResult(TypeDonnees.EMPLOYE, fenetre.arEmploye.get(i)));
                ajoutResultat2(new SearchResult(TypeDonnees.EMPLOYE, fenetre.arEmploye.get(i)));
            } else if (fenetre.arEmploye.get(i).getInitiales().toLowerCase().contains(input)) {
                //ajoutResultat(new SearchResult(TypeDonnees.EMPLOYE, fenetre.arEmploye.get(i)));
                ajoutResultat2(new SearchResult(TypeDonnees.EMPLOYE, fenetre.arEmploye.get(i)));
            }
        }
    }

    private void searchEntreeClient() {
        for (int i = 0; i < fenetre.arEntree.size(); i++) {
            if (fenetre.arEntree.get(i).getClient().getNom().toLowerCase().contains(input)) {
                ajoutResultat2(new SearchResult(TypeDonnees.ENTREE, fenetre.arEntree.get(i)));
            } else if (fenetre.arEntree.get(i).getClient().getCode().toLowerCase().contains(input)) {
                ajoutResultat2(new SearchResult(TypeDonnees.ENTREE, fenetre.arEntree.get(i)));
            }
        }
    }

    private void searchEntreeEmployé() {
        for (int i = 0; i < fenetre.arEntree.size(); i++) {
            if (fenetre.arEntree.get(i).getEmploye().getNom().toLowerCase().contains(input)) {
                ajoutResultat2(new SearchResult(TypeDonnees.ENTREE, fenetre.arEntree.get(i)));
            } else if (fenetre.arEntree.get(i).getEmploye().getInitiales().toLowerCase().contains(input)) {
                ajoutResultat2(new SearchResult(TypeDonnees.ENTREE, fenetre.arEntree.get(i)));
            }
        }
    }

    private void searchFactureNumero() {
        for (int i = 0; i < fenetre.arFactures.size(); i++) {
            if (fenetre.arFactures.get(i).getNumero().toUpperCase().contains(input.toUpperCase())) {
                ajoutResultat2(new SearchResult(TypeDonnees.FACTURE, fenetre.arFactures.get(i)));
            }
        }
    }
    private void searchFactureClient() {
        for (int i = 0; i < fenetre.arFactures.size(); i++) {
            if (fenetre.arFactures.get(i).getClient().getCode().contains(input)) {
                ajoutResultat2(new SearchResult(TypeDonnees.FACTURE, fenetre.arFactures.get(i)));
            }
        }
    }

    private void makeSeparator() {
        JLabel sep = new JLabel(" ");
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(sep, gbc);
        this.add(sep);
        gbc.gridwidth = gbc.CENTER;
        gbc.weighty = 0;
    }

    private void makeBorder() {
        gbc.weightx = 0.1;
        JLabel sep = new JLabel(" ");
        gbl.setConstraints(sep, gbc);
        this.add(sep);
        gbc.gridwidth = gbc.CENTER;
    }

    private void ajoutResultat(SearchResult sr) {
        arrResultats.add(sr);
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(sr, gbc);
        pnlVisible.add(sr);
    }

    private void ajoutResultat2(SearchResult sr) {
        arrResultats2.add(sr);
    }

    public ArrayList<SearchResult> getArrResultats2() {
        return arrResultats2;
    }

    public ArrayList<SearchResult> getListeResultats() {
        return arrResultats;
    }
}
